<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- 引入react核心库 -->
    <script src="https://unpkg.com/react@17/umd/react.development.js"></script>
    <!-- 引入react-dom，用于支持react操作DOM -->
    <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
    <!-- 引入babel，用于将jsx转为js -->
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    <!-- 引入prop-types -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.8.1/prop-types.min.js"></script>
    <title>02-组件生命周期旧-初始化阶段</title>
</head>

<body>
    <!-- 准备好一个"容器" -->
    <div id="root"></div>
    <script type="text/babel">
        //创建组件
        //生命周期回调函数 <=> 生命周期钩子函数 <=> 生命周期函数 <=> 生命周期钩子
        class Count extends React.Component {
            constructor(props) {
                alert('constructor')
                console.log('Count---constructor')
                super(props)
                this.state = { count: 0 }
            }

            add = () => {
                const { count } = this.state
                this.setState({ count: count + 1 })
            }

            // 组件将要挂载的钩子
            componentWillMount() {
                alert('componentWillMount')
                console.log('Count---componentWillMount')
            }

            //初始化渲染、状态更新之后
            render() {
                alert('render')
                console.log('Count---render');
                const { count } = this.state
                return (
                    <div>
                        <h1>当前计数值: {count}</h1>
                        <button onClick={this.add}>点我+1</button>
                    </div>
                )
            }

            componentDidMount() {
                alert('componentDidMount')
                console.log('Count---componentDidMount')
            }
        }

        //渲染组件
        ReactDOM.render(<Count />, document.getElementById('root'))
    </script>
</body>

</html>